--[[
	
	*****************************************************************************
	*                                                                           *
	*  PROJECT:     User Interface                                              *
	*  LICENSE:     See LICENSE in the top level directory                      *
	*  FILE:        Main.lua                                                    *
	*  DEVELOPERS:  Cadu12                                                      *
	*  CREDITS:     Remi-X                                                      *
	*               FabioGNR                                                    *
	*               Pugwipe                                                     *
	*               ccw                                                         *
	*                                                                           *
	*  Multi Theft Auto is available from http://www.multitheftauto.com/        *
	*  User Interface is available from                                         *
	*                                                                           *
	*****************************************************************************
	
]]--

font = nil

addEventHandler ( "onClientResourceStart", getResourceRootElement ( getThisResource ( ) ),
function ( )
	
	UI_CreateTheme ( "LighterBlack", "Themes/LighterBlack/CGUI.png", "Themes/LighterBlack/CGUI.is.xml", true )
	UI_CreateTheme ( "LighterBlue", "Themes/LighterBlue/CGUI.png", "Themes/LighterBlue/CGUI.is.xml" )
	UI_CreateTheme ( "SathMetro", "Themes/SathMetro/CGUI.png", "Themes/SathMetro/CGUI.is.xml" )
	UI_CreateTheme ( "Orange", "Themes/Orange/CGUI.png", "Themes/Orange/CGUI.is.xml" )

	if ( UI_Debug ) then
		
		font = UI_CreateFont("font.ttf", 1, false)
		--UI_FontSetSize (font, 50)

		local windows = UI_CreateWindow("Inform#00ff00ation", 10, 200, 250, 500)
		local button = UI_CreateButton("Taa", 0, 0, 100, 100, windows)
		local label = UI_CreateLabel("Testing", 0, 120, 150, 40, windows)
		local checkbox = UI_CheckBox("TEsting", true, 20, 150, 150, 50, windows)
		local radiobutton1 = UI_RadioButton("Aaaaaa", 0, 250, 100, 50, windows)
		local radiobutton2 = UI_RadioButton("Aaaaaa", 0, 300, 100, 50, windows)
		local progress = UI_CreateProgressBar(0, 400, 200, 50, windows)
		UI_SetColorCoded(windows, true)
		UI_SetFont(windows, font)
		UI_SetFont(button, font)
		UI_SetFont(label, font)
		UI_SetFont(checkbox, font)
		UI_SetFont(radiobutton1, font)
		UI_SetFont(radiobutton2, font)
		UI_FontSetSize(font, 24)
		UI_ProgressBarSetProgress ( progress, 500 )

		UI_SetDefaultTheme ( "LighterBlue" )

		windows = UI_CreateWindow("Information2", 270, 200, 250, 500)
		UI_CreateButton("Taa", 0, 0, 100, 100, windows)
		UI_CreateLabel("Testing", 0, 120, 150, 40, windows)
		UI_CheckBox("TEsting", true, 20, 150, 150, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 250, 100, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 300, 100, 50, windows)
		local progress = UI_CreateProgressBar(0, 400, 200, 20, windows)
		UI_ProgressBarSetProgress ( progress, 100 )
		UI_SetDefaultTheme ( "Orange" )

		windows = UI_CreateWindow("Information3", 530, 200, 250, 500)
		button = UI_CreateButton("Taa", 0, 0, 100, 100, windows)
		UI_CreateLabel("Testing", 0, 120, 150, 40, windows)
		UI_CheckBox("TEsting", true, 20, 150, 150, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 250, 100, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 300, 100, 50, windows)
		UI_CreateProgressBar(0, 400, 200, 50, windows)
		UI_SetFont(button, font)

		UI_SetDefaultTheme ( "SathMetro" )

		windows = UI_CreateWindow("Information3", 790, 200, 250, 500)
		UI_CreateButton("Taa", 0, 0, 100, 100, windows)
		UI_CreateLabel("Testing", 0, 120, 150, 40, windows)
		UI_CheckBox("TEsting", true, 20, 150, 150, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 250, 100, 50, windows)
		UI_RadioButton("Aaaaaa", 0, 300, 100, 50, windows)
		UI_CreateProgressBar(0, 400, 200, 50, windows)

		showCursor(true)
	end

end )

addEventHandler ( "onClientResourceStop", getRootElement ( ),
function ( )
	for _, type in ipairs ( { "UI_Window" } ) do
		for _, v in ipairs ( getElementsByType ( type, source ) ) do
			UI_TitleFont[v] = nil
			destroyElement ( v )
			if ( UI_RenderParent[v] ) then
				for _, eleparent in ipairs ( UI_RenderParent[v] ) do
					if ( isElement ( eleparent ) ) then
						destroyElement ( eleparent )
					end
				end
			end
			for _, ele in ipairs ( UI_Render ) do
				if ( ele == v ) then
					table.remove ( UI_Render, i )
					UI_RenderParent[v] = nil
					break
				end
			end
		end
	end
end )

addEventHandler ( "onClientClick", getRootElement ( ), 
function ( Button, State, cX, cY )
	local target = UI_isMouseInElements ( cX, cY )
	
	if ( UI_isElement ( target ) and UI_GetVisible ( target ) ) then

		if ( Button == "left" and State == "up" and UI_Selected == target ) then
			if ( getElementType ( target ) == "UI_CheckBox" ) then
				UI_CheckBoxSetSelected ( target, not UI_CheckBoxGetSelected ( target) )
			elseif ( getElementType ( target ) == "UI_RadioButton" ) then
				UI_RadioButtonSetSelected ( target, true )
			end
		end

		triggerEvent ( "onUI_Click", target, Button, State, cX, cY )
		if ( Button == "left" and State == "down" ) then
			UI_Selected = target
		else
			UI_Selected = nil
		end 
	else
		UI_Selected = nil
	end

	if ( Button == "left" and State == "down" ) then
		local ele = UI_Render[#UI_Render]
		if ( UI_isElement ( ele ) and UI_GetVisible ( ele ) ) then			
			local X = getElementData ( ele, "UI_X" ) or 0
			local Y = getElementData ( ele, "UI_Y" ) or 0
			local Width = getElementData ( ele, "UI_Width" ) or 0
			local Height = getElementData ( ele, "UI_Height" ) or 0
			local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
			local ThemeHeight = UI_ThemeGetHeight ( "TitleBarBackground", Theme )
			if ( cX >= X and cX <= X + Width ) and ( cY >= Y and cY <= Y + Width ) then
				if ( getElementType ( ele ) == "UI_Window" ) and ( cX >= X and cX <= X + Width ) and ( cY >= Y and cY <= Y + ThemeHeight ) then
					UI_Offset["X"] = ( X - cX )
					UI_Offset["Y"] = ( Y - cY )
					UI_IsMoving = ele
				end
				if ( getElementType ( ele ) == "UI_Window" ) then
					UI_WindowSelected = ele
				end
			else
				for i = 1, #UI_Render do
					local ele = UI_Render[( #UI_Render + 1 ) - i]
					if ( UI_isElement ( ele ) and UI_GetVisible ( ele ) ) then
						local X = getElementData ( ele, "UI_X" ) or 0
						local Y = getElementData ( ele, "UI_Y" ) or 0
						local Width = getElementData ( ele, "UI_Width" ) or 0
						local Height = getElementData ( ele, "UI_Height" ) or 0
						local Theme = getElementData ( ele, "UI_Theme" ) or UI_ThemeDefault
						local ThemeHeight = UI_ThemeGetHeight ( "TitleBarBackground", Theme )
						if ( cX >= X and cX <= X + Width ) and ( cY >= Y and cY <= Y + Height ) then
							if ( getElementType ( ele ) == "UI_Window" ) and ( cX >= X and cX <= X + Width ) and ( cY >= Y and cY <= Y + ThemeHeight ) then
								UI_Offset["X"] = ( X - cX )
								UI_Offset["Y"] = ( Y - cY )
								UI_IsMoving = ele
							end
							for i, v in ipairs ( UI_Render ) do
								if ( ele == v ) then
									table.remove ( UI_Render, i )
									break
								end
							end
							table.insert ( UI_Render, ele )
							if ( getElementType ( ele ) == "UI_Window" ) then
								UI_WindowSelected = ele
							end
							break
						end
					end
				end
			end
		end
	else
		UI_IsMoving = nil
	end
end )

addEventHandler ( "onClientDoubleClick", getRootElement ( ), 
function ( Button, cX, cY )	
	local Target = UI_isMouseInElements ( cX, cY )
	if ( UI_isElement ( Target ) and UI_GetVisible ( Target ) ) then
		triggerEvent ( "onUI_DoubleClick", Target, Button, cX, cY )
	end
end )

addEventHandler ( "onClientRender", getRootElement( ),
function ( )

	if ( UI_Debug ) then
		local X, Y = guiGetScreenSize()
		dxDrawRectangle ( 0, 0, X, Y, tocolor ( 140, 187, 252, 255 ) )
	end

	for _, ele in ipairs ( UI_Render ) do
		if ( UI_isElement ( ele ) and UI_GetVisible ( ele ) ) then
			if ( getElementType ( ele ) == "UI_Window" ) then
				UI_RenderWindow ( ele )
			elseif ( getElementType ( ele ) == "UI_Label" ) then
				UI_RenderLabel ( ele )
			end
		end
	end

	if ( not isCursorShowing ( ) ) then return end
	local cX, cY = getCursorPosition ( )
	cX = cX * ScreenX
	cY = cY * ScreenY

	UI_CheckMouseOutElements(cX, cY)

	local OldTarget = UI_OldElement
	local Target = UI_isMouseInElements ( cX, cY )

	if ( UI_isElement ( Target ) and UI_GetVisible ( Target ) ) then
		local MouseState = getElementData ( Target, "UI_EventMouseState" ) or false
		if ( not MouseState ) then
			setElementData ( Target, "UI_EventMouseState", true )
			triggerEvent ( "onUI_MouseEnter", Target, cX, cY )
			if ( isElement ( OldTarget ) and OldTarget ~= Target ) then
				MouseState = getElementData ( OldTarget, "UI_EventMouseState" )
				if ( MouseState ) then
					setElementData ( OldTarget, "UI_EventMouseState", false )
					triggerEvent ( "onUI_MouseLeave", OldTarget, cX, cY )
				end
			end
		end
		if ( UI_OldCursor["X"] ~= cX or UI_OldCursor["Y"] ~= cY ) then
			triggerEvent ( "onUI_MouseMove", Target, cX, cY )
			UI_OldCursor["X"] = cX
			UI_OldCursor["Y"] = cY
		end
	end
end )

addEventHandler ( "onClientCharacter", getRootElement ( ), 
function ( char )

end )

if ( UI_Debug ) then

	addEvent("onUI_MouseEnter", true)
	addEventHandler( "onUI_MouseEnter", getRootElement(), 
		function(aX, aY)
			local text = " "
			if getElementType( source ) == "UI_Window" then
				text = getElementData ( source, "UI_Title" ) or " "
			else
				text = getElementData ( source, "UI_Text" ) or " "
			end
			outputChatBox( text .. ": onUI_MouseEnter ("..tostring(aX)..", "..tostring(aY)..")")
		end
	)

	addEvent("onUI_MouseLeave", true)
	addEventHandler( "onUI_MouseLeave", getRootElement(), 
		function(aX, aY)
			local text = " "
			if getElementType( source ) == "UI_Window" then
				text = getElementData ( source, "UI_Title" ) or " "
			else
				text = getElementData ( source, "UI_Text" ) or " "
			end
			outputChatBox( text .. ": onUI_MouseLeave ("..tostring(aX)..", "..tostring(aY)..")")
		end
	)

	--[[addEvent("onUI_MouseMove", true)
	addEventHandler( "onUI_MouseMove", getRootElement(), 
		function(aX, aY)
			local text = " "
			if getElementType( source ) == "UI_Window" then
				text = getElementData ( source, "UI_Title" ) or " "
			else
				text = getElementData ( source, "UI_Text" ) or " "
			end
			outputChatBox( text .. ": onUI_MouseMove ("..tostring(aX)..", "..tostring(aY)..")")
		end
	)]]--

	addEvent("onUI_Click", true)
	addEventHandler( "onUI_Click", getRootElement(), 
		function(button, state, aX, aY)
			local text = " "
			if getElementType( source ) == "UI_Window" then
				text = getElementData ( source, "UI_Title" ) or " "
			else
				text = getElementData ( source, "UI_Text" ) or " "
			end
			outputChatBox( text .. ": onUI_Click ("..tostring(aX)..", "..tostring(aY)..")")
		end
	)

	addEvent("onUI_DoubleClick", true)
	addEventHandler( "onUI_DoubleClick", getRootElement(), 
		function(button, aX, aY)
			local text = " "
			if getElementType( source ) == "UI_Window" then
				text = getElementData ( source, "UI_Title" ) or " "
			else
				text = getElementData ( source, "UI_Text" ) or " "
			end
			outputChatBox( text .. ": onUI_DoubleClick ("..tostring(aX)..", "..tostring(aY)..")")
		end
	)

end